package org.agridata.akvisualization.controller.xuqin;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.agridata.akvisualization.doMain.vo.AnimalSaveCropVo;
import org.agridata.akvisualization.result.Result;
import org.agridata.akvisualization.service.AnimalSaveCropService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author: yxc
 * @date: 2024/2/1 15:04
 * @Description: 动物出栏存栏类
 */
@RestController
@RequestMapping("/animalSaveCrop")
@Api(tags = "动物出栏存栏")
public class AnimalSaveCropController {

    @Autowired
    AnimalSaveCropService animalSaveCropService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("出栏存栏导入")
    @PostMapping("livestockImport")
    public Result animalSaveCropImport(@RequestPart("file") MultipartFile file, String dates, String quarter){
        try {
            List<AnimalSaveCropVo> memberList = EasyExcel.read(file.getInputStream())
                    .head(AnimalSaveCropVo.class)
                    .sheet()
                    .doReadSync();
            return animalSaveCropService.importAnimalSaveCrop(memberList,dates,quarter);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("出现异常");
        }
    }

    /**
     *
     * @param data 日期
     * @param quarter 季度类型
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("存栏出栏列表 --导入数据表格的数据")
    @GetMapping("/getAnimalList")
    public Result getAnimalList(String data,String quarter,Long pageNum,Long pageSize){
        try {
            Page<AnimalSaveCropVo> pageData = animalSaveCropService.getAnimalList(data,quarter,pageNum,pageSize);
            return Result.ok(pageData);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询失败");
        }
    }

    /**
     *
     * @param type 当type=1时  展示导入数据  当type=2时 展示正式环境库的数据
     * @return
     */
    @ApiOperation("首页展示存栏出栏")
    @GetMapping("/getAnimalRatio")
    public Result getAnimalRatio(String type){
        Object animalRatioKey = new Object();
        try {
            if (type.contains("1")){
                animalRatioKey = redisTemplate.opsForValue().get("getAnimalRatioKey1");
            }
            if (type.contains("2")){
                animalRatioKey = redisTemplate.opsForValue().get("getAnimalRatioKey2");
            }

            if (animalRatioKey != null){
                return Result.ok(animalRatioKey);
            }else {
                Map<String, Object> pageData = animalSaveCropService.getAnimalRatio(type);
                if (type.contains("1")){
                    redisTemplate.opsForValue().set("getAnimalRatioKey1",pageData);
                    // 设置 key 的过期时间，例如设置为 1 小时
                    redisTemplate.expire("getAnimalRatioKey1", 5, TimeUnit.HOURS);
                }
                if (type.contains("2")){
                    redisTemplate.opsForValue().set("getAnimalRatioKey2",pageData);
                    // 设置 key 的过期时间，例如设置为 1 小时
                    redisTemplate.expire("getAnimalRatioKey2", 5, TimeUnit.HOURS);
                }
                return Result.ok(pageData);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询失败");
        }
    }


}
